package com.xujl.utilslibrary.paramcorrect;

import com.xujl.utilslibrary.data.TypeUtils;
import com.xujl.utilslibrary.view.ViewTool;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by xujl on 2018/1/19.
 * String和Int字段值修正
 */

public class ParamCorrectUtils {
    private HashMap<Integer, Object> defaultValueMap = new HashMap<>();

    /**
     * 添加默认修正值，每种类型只能设置一个
     *
     * @param type
     * @param value
     * @return
     */
    public ParamCorrectUtils addValue (int type, Object value) {
        defaultValueMap.put(type, value);
        return this;
    }

    public void correctAll (Object obj) throws IllegalAccessException {

        final Class<?> objClass = obj.getClass();
        //如果为集合类型，进行遍历修正
        if (obj instanceof List) {
            List list = (List) obj;
            for (Object o : list) {
                correctAll(o);
            }
            return;
        }
        //获取所有字段
        final Field[] fields = objClass.getDeclaredFields();
        //需要修正的字段
        final List<Field> correctFieldList = new ArrayList<>();
        for (Field field : fields) {
            final Correct annotation = field.getAnnotation(Correct.class);
            if (annotation != null) {
                correctFieldList.add(field);
            }
        }
        run(correctFieldList, obj);
    }

    /**
     * 开始修正
     *
     * @param correctFieldList
     */
    private void run (List<Field> correctFieldList, Object obj) throws IllegalAccessException {
        for (Field field : correctFieldList) {
            final CorrectValue annotation = field.getAnnotation(CorrectValue.class);
            final int type = TypeUtils.getType(field.getType().getName());
            field.setAccessible(true);

            if (type == TypeUtils.TYPE_INT) {
                final int value = field.getInt(obj);
                if (value != 0) {
                    continue;
                }
                if (annotation != null) {
                    field.setInt(obj, annotation.intValue());
                    continue;
                }
                if (defaultValueMap.containsKey(TypeUtils.TYPE_INT)) {
                    field.setInt(obj, (Integer) defaultValueMap.get(TypeUtils.TYPE_INT));
                }
                continue;
            }
            if (type == TypeUtils.TYPE_STRING) {
                final String value = (String) field.get(obj);
                if (!ViewTool.isEmpty(value)) {
                    continue;
                }
                if (annotation != null) {
                    field.set(obj, annotation.StringValue());
                    continue;
                }
                if (defaultValueMap.containsKey(TypeUtils.TYPE_STRING)) {
                    field.set(obj, defaultValueMap.get(TypeUtils.TYPE_STRING));
                }
                continue;
            }
            //值为list时，进行递归
            if (type == TypeUtils.TYPE_LIST) {
                correctAll(field.get(obj));
            }
        }

    }


}
